package net.template.service;

import net.template.domain.Column;
import net.template.domain.ColumnAttr;
import net.template.domain.Item;
import net.template.repository.ColumnAttrRepository;
import net.template.repository.ColumnRepository;
import net.template.repository.ItemRepository;
import net.template.service.controller.BaseController;
import net.template.service.controller.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 栏目
 * @author zuowenjie
 */
@RestController
@CrossOrigin
@RequestMapping("column")
public class ColumnService extends BaseController {


    @Autowired
    private ColumnRepository columnRepository;
    @Autowired
    private ColumnAttrRepository columnAttrRepository;
    @Autowired
    private ItemRepository itemRepository;

    /**
     * 新增栏目
     * @param input
     * @return
     */
    @Transactional
    @PostMapping
    public Result<Void> createColumn(@RequestBody Column input){
        Column column = new Column();
        column.createVerify(input);
        columnRepository.save(column);
        return Result.success();
    }

    /**
     * 删除栏目
     * @param id
     * @return
     */
    @Transactional
    @DeleteMapping("/{id}")
    public Result<Void> deleteColumn(@PathVariable("id") Integer id){

        Column column = columnRepository.load(id);
        List<ColumnAttr> columnAttrList = columnAttrRepository.findByColumnId(id);

        columnRepository.delete(column);
        if(columnAttrList != null && !columnAttrList.isEmpty()){
            for(ColumnAttr columnAttr : columnAttrList){
                columnAttrRepository.delete(columnAttr);
                Item item = itemRepository.findByColumnAttrId(columnAttr.getColumnId());
                itemRepository.delete(item);
            }
        }
        return Result.success();
    }

    /**
     * 编辑指定的栏目
     * @return
     */
    @PutMapping("/{id}")
    public Result<Map<String, Object>> modifyColumn(@PathVariable("id") Integer id, @RequestBody Map<String,
                                                    Object> input) {
        Column column = new Column();
        column.setColumnName((String) input.get("columnName"));
        column.setColumnDesc((String) input.get("columnDesc"));
        ColumnAttr columnAttr = new ColumnAttr();
        columnAttr.setAttrKey((String)input.get("attrKey"));
        columnAttr.setAttrCn((String)input.get("attrCn"));
        columnAttr.setAttrType((String)input.get("attrType"));
        columnAttr.setColumnId(id);

        columnAttr.setColumnId(id);
        Column column1 = columnRepository.load(id);
        column1.createVerify(column);
        columnRepository.save(column1);
        return Result.success(null);
    }

    /**
     * 栏目列表
     * @return
     */
    @GetMapping
    public Result<List<Column>> queryColumn(){
        List<Column> columnList = columnRepository.findAll(new Specification<Column>() {
            @Override
            public Predicate toPredicate(Root<Column> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.isNotNull(root.get("id"));
                return predicate;
            }
        });

        return Result.success(columnList);
    }

    /**
     * 查询指定栏目详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Map<String, Object>> querySpecialColumn(@PathVariable("id") Integer id){
        Column column = columnRepository.load(id);
        List<ColumnAttr> columnAttrList = columnAttrRepository.findByColumnId(id);
        for(ColumnAttr columnAttr : columnAttrList){
            Item item = itemRepository.findByColumnAttrId(columnAttr.getColumnId());
            columnAttr.setItem(item);
        }
        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("column", column);
        objectMap.put("columnAttr", columnAttrList);
        return Result.success(objectMap);
    }

    /**
     * 为栏目的每个标签设置样式
     * @param input
     * @return
     */
    @Transactional
    @PostMapping("/template")
    public Result<Void> createTemplate(Map<String, Object> input){
        Integer columnId = (Integer) input.get("columnId");
        List<Map<String, Object>> columnAttrs = new ArrayList<>();
        columnAttrs = (List<Map<String, Object>>) input.get("columnAttrs");
        for(Map<String, Object> map : columnAttrs){
            Item item = new Item();
            Map<String, String> items = new HashMap<>();
            items = (Map<String, String>) map.get("items");
            item.createItem(items.get("itemType"), items.get("itemLabel"), items.get("itemDisable"), items.get("itemReadonly"), items.get("itemValue"), items.get("itemPlaceholder"), items.get("itemKey"), items.get("itemSubtype"), items.get("itemText"), items.get("itemRules"), Integer.parseInt(items.get("columnAttrId")));
            itemRepository.save(item);
        }

        return Result.success();
    }

}
